<?php

namespace app\iot\controller;

use Throwable;
use app\common\controller\Frontend;
use think\facade\Log;
use think\Response;
use think\facade\Db;
use think\db\exception\PDOException;
use think\exception\HttpResponseException;
class Gateway extends Frontend
{
    protected array $noNeedLogin = ['work'];

    public function initialize(): void
    {
        parent::initialize();
    }

    public function work(){
        $content = $this->request->getContent();
        Log::info('content:'.$content);
        if($this->request->isPost()){
            $type = $this->request->param('Type','1');
            $mac = $this->request->param('MAC','');
            $device = $this->request->param('DeviceID','');
            $data = $this->request->param('Data','');
            //content:{"Type":"1","MAC":"383B263757BC","DeviceID":"ronghetiyu370321","Data":""}
            if($mac == '383B263757BC' && $device == 'ronghetiyu370321'){
                if($type == '0'){
                    if($data == '0202010000000303'){//接收回调
                        return '';
                    }
                    if($data && substr($data,0,1)==1){//成功验证
                        return '0202F2AC000203E8B503';
                    }else{
                        return '0202F2AD0001035F03';//失败返回
                    }
                }
                if($type == '1'){
                    return '';
                }
            }
            return '';
        }
    }
}